package com.xk._04regEx;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: Matcher 类的常用方法
 * @author: xu
 * @date: 2022/11/2 11:18
 */
public class MatcherMethod {
    public static void main(String[] args) {
        String content = "hello edu jack hello smith hello";
        String regStr = ".*hello";

        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("===============================");
            System.out.println(matcher.start());
            System.out.println(matcher.end());
            System.out.println("找到：" + content.substring(matcher.start(), matcher.end()));
        }

        // 整体匹配方法，常用于 去校验某个字符串是否满足某个规则
        System.out.println("整体匹配：" + matcher.matches());

        System.out.println("==========ReplaceAll============");
        // 完成如果 content 中有 hello，将 hello 替换成 韩顺平教育
        regStr = "hello";
        pattern = Pattern.compile(regStr);
        matcher = pattern.matcher(content);
        String newContent = matcher.replaceAll("韩顺平教育");
        System.out.println("content = " + content);
        System.out.println("newContent = " + newContent);
    }
}
